/*
 * Created by JFormDesigner on Mon Mar 28 22:20:34 CST 2022
 */

package com.wangcanxuan.www.view.tribe;

import java.awt.event.*;

import com.wangcanxuan.www.controller.ApplyController;
import com.wangcanxuan.www.controller.Impl.ApplyControllerImpl;
import com.wangcanxuan.www.controller.Impl.LikeControllerImpl;
import com.wangcanxuan.www.controller.Impl.TribeControllerImpl;
import com.wangcanxuan.www.controller.LikeController;
import com.wangcanxuan.www.controller.TribeController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Tribe;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 查看审核界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class CheckApplyTribeView extends JFrame {
    private final User user;
    private Tribe tribe;
    private int page = 1;
    private String chooseUserName;
    private final TribeController tribeController;
    private final LikeController likeController;
    private final ApplyController applyController;

    public CheckApplyTribeView(User user) {
        tribeController = ControllerProxy.getProxyInstance(TribeControllerImpl.class);
        likeController = ControllerProxy.getProxyInstance(LikeControllerImpl.class);
        applyController = ControllerProxy.getProxyInstance(ApplyControllerImpl.class);
        this.user = user;
        initComponents();
        setVisible(true);
        initMessage();
    }

    private void initMessage() {
        pageText.setText(String.valueOf(1));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        fillApplyTable(0);
    }

    private boolean fillApplyTable(int pageCount) {
        ResultInfo<List<User>> listResultMessage = applyController.selectApplyListByTribeId(user.getTribeId(), pageCount);
        List<User> users = listResultMessage.getObj();
        int code = listResultMessage.getCode();
        String message = listResultMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            DefaultTableModel dtm = (DefaultTableModel) applyTable.getModel();
            // 设置成0行
            dtm.setRowCount(0);
            for (User selectUser : users) {
                Vector<Object> list = new Vector<>();
                list.add(selectUser.getNickName());
                list.add(selectUser.getGender());
                list.add(selectUser.getAge());
                ResultInfo<Long> longResultMessage = likeController.selectLikesForUser(user.getId());
                list.add(longResultMessage.getObj());
                list.add(selectUser.getCoins());
                dtm.addRow(list);
            }
            return true;
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            JOptionPane.showMessageDialog(null, message);
            ResultInfo<Tribe> tribeResultMessage = tribeController.selectTribeByTribeId(user.getTribeId());
            tribe = tribeResultMessage.getObj();
            new TribalInterfaceView(user, tribe);
            dispose();
            return false;
        }
        return false;
    }

    /**
     * 回到部落
     *
     * @param e e
     */
    private void backTribe(ActionEvent e) {
        // TODO add your code here
        ResultInfo<Tribe> tribeResultMessage = tribeController.selectTribeByTribeId(user.getTribeId());
        tribe = tribeResultMessage.getObj();
        new TribalInterfaceView(user, tribe);
        dispose();
    }

    /**
     * 刷新
     *
     * @param e e
     */
    private void refresh(ActionEvent e) {
        // TODO add your code here
    }

    /**
     * 上一页
     *
     * @param e e
     */
    private void upPage(ActionEvent e) {
        // TODO add your code here
        if (page == 1) {
            JOptionPane.showMessageDialog(null, "这是首页！");
            return;
        }
        int count = (page % 10) - 1;
        boolean b = fillApplyTable(count);
        if (b) {
            page--;
            pageText.setText(String.valueOf(page));
        }
    }

    /**
     * 下页面
     *
     * @param e e
     */
    private void downPage(ActionEvent e) {
        // TODO add your code here
        int count = page * 10;
        boolean b = fillApplyTable(count);
        if (b) {
            page++;
            pageText.setText(String.valueOf(page));
        }
    }

    private void applyTableMousePressed(MouseEvent e) {
        // TODO add your code here
        int selectedRow = applyTable.getSelectedRow();
        chooseUserName = String.valueOf(applyTable.getValueAt(selectedRow, 0));
        chooseUserText.setText(chooseUserName);
    }

    private void accept(ActionEvent e) {
        // TODO add your code here
        solveApply(1);
    }

    private void reject(ActionEvent e) {
        // TODO add your code here
        solveApply(0);
    }

    private void solveApply(int status) {
        ResultInfo<?> resultMessage = applyController.solveApply(chooseUserName, user, status);
        String message = resultMessage.getMessage();
        int code = resultMessage.getCode();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new CheckApplyTribeView(user);
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        backTribeButton = new JButton();
        scrollPane1 = new JScrollPane();
        applyTable = new JTable();
        upPageButton = new JButton();
        pageText = new JTextField();
        downPageButton = new JButton();
        chooseUserText = new JTextField();
        label2 = new JLabel();
        acceptButton = new JButton();
        rejectButton = new JButton();
        refreshButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5ba1\u6838");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- backTribeButton ----
        backTribeButton.setText("\u8fd4\u56de\u90e8\u843d");
        backTribeButton.addActionListener(e -> backTribe(e));

        //======== scrollPane1 ========
        {

            //---- applyTable ----
            applyTable.setModel(new DefaultTableModel(
                    new Object[][]{
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                            {null, null, null, null, null},
                    },
                    new String[]{
                            "\u6635\u79f0", "\u6027\u522b", "\u5e74\u9f84", "\u88ab\u8d5e\u6570", "\u80fd\u4e0a\u7f34\u7684\u91d1\u5e01\u6570"
                    }
            ) {
                boolean[] columnEditable = new boolean[]{
                        false, false, false, false, false
                };

                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return columnEditable[columnIndex];
                }
            });
            applyTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    applyTableMousePressed(e);
                }
            });
            scrollPane1.setViewportView(applyTable);
        }

        //---- upPageButton ----
        upPageButton.setText("\u4e0a\u4e00\u9875");
        upPageButton.addActionListener(e -> upPage(e));

        //---- downPageButton ----
        downPageButton.setText("\u4e0b\u4e00\u9875");
        downPageButton.addActionListener(e -> downPage(e));

        //---- label2 ----
        label2.setText("\u5f53\u524d\u9009\u62e9\u7528\u6237\uff1a");

        //---- acceptButton ----
        acceptButton.setText("\u540c\u610f");
        acceptButton.addActionListener(e -> accept(e));

        //---- rejectButton ----
        rejectButton.setText("\u62d2\u7edd");
        rejectButton.addActionListener(e -> reject(e));

        //---- refreshButton ----
        refreshButton.setText("\u5237\u65b0");
        refreshButton.addActionListener(e -> refresh(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(105, 105, 105)
                                                .addComponent(upPageButton, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                                                .addGap(66, 66, 66)
                                                .addComponent(pageText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(66, 66, 66)
                                                .addComponent(downPageButton, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(62, 62, 62)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 502, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(22, 22, 22)
                                                                .addComponent(chooseUserText, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(acceptButton, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                                .addComponent(rejectButton, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(69, Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addComponent(backTribeButton)
                                .addGap(153, 153, 153)
                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
                                .addComponent(refreshButton, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                                .addGap(79, 79, 79))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(19, 19, 19)
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                        .addComponent(label1, GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                                                        .addComponent(refreshButton)))
                                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                                .addContainerGap(26, Short.MAX_VALUE)
                                                .addComponent(backTribeButton, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                .addComponent(chooseUserText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(rejectButton)
                                                .addComponent(acceptButton))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(5, 5, 5)
                                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)))
                                .addGap(9, 9, 9)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 238, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(upPageButton)
                                        .addComponent(pageText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(downPageButton))
                                .addContainerGap(41, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JButton backTribeButton;
    private JScrollPane scrollPane1;
    private JTable applyTable;
    private JButton upPageButton;
    private JTextField pageText;
    private JButton downPageButton;
    private JTextField chooseUserText;
    private JLabel label2;
    private JButton acceptButton;
    private JButton rejectButton;
    private JButton refreshButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
